<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\facade\Db;

/**
 * @mixin \think\Model
 */
class ArticleCat extends Model
{
    //文章类型列表
    public static function list($where = "")
    {
        $res = Db::name('article_cat')->where($where)->where('del', '0')->order('px')->select();
        return $res;
    }

    //文章类型添加
    public static function add($data)
    {
        $data['add_date'] = time();
        $res = Db::name('article_cat')->insert($data);
        return $res;
    }

    //文章类型编辑
    public static function edit($data)
    {
        $data['rep_date'] = time();
        $res = Db::name('article_cat')->update($data);
        return $res;
    }

    //文章类型删除
    public static function del($id)
    {
        $son = Db::name('article_cat')->where('parent_id', $id)->where('del', '0')->find();
        if ($son) {
            return '请先删除下级!';
        } else {
            $art = Db::name('article')->where('cat_id', $id)->where('del', '0')->find();
            if ($art) {
                return '该分类已有文章，请先删除该类文章!';
            } else {
                $res = Db::name('article_cat')->where('id', $id)->update(['del' => '1', 'rep_date' => time()]);
                if ($res) {
                    return 'success';
                } else {
                    return '删除失败';
                }
            }
        }
    }
}
